package com.shinhye.smartsms;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.shinhye.smartsms.db.GroupOpenHelper;

public class GroupDBProvider extends ContentProvider {
	private static final String AUTHORITY = "com.shinhye.smartsms";
	private static final Uri NOTIFY_URI = Uri.parse("content://com.shinhye.smartsms");
	private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
	private static final int GROUP_CODE = 0;
	private static final int GROUP_ID_CODE = 1;
	private static final int THREAD_ID_CODE = 2;
	private GroupOpenHelper goh;
	private SQLiteDatabase db;
	static{
		matcher.addURI(AUTHORITY, "groups", GROUP_CODE);
		matcher.addURI(AUTHORITY, "groups/#", GROUP_ID_CODE);
		matcher.addURI(AUTHORITY, "thread_group", THREAD_ID_CODE);
	}
	
	@Override
	public boolean onCreate() {
		goh = GroupOpenHelper.getInstance(getContext());
		db = goh.getReadableDatabase();
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Cursor cursor;
		switch(matcher.match(uri)) {
		case GROUP_CODE:
			cursor = db.query("groups", projection, selection, selectionArgs, null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), NOTIFY_URI);
			break;
		case THREAD_ID_CODE:
			cursor = db.query("thread_group", projection, selection, selectionArgs, null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), NOTIFY_URI);
			break;
		default:
			throw new IllegalArgumentException("无效的URI: " + uri);
		}
		return cursor;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		switch(matcher.match(uri)) {
		case GROUP_CODE:
			long id1 = db.insert("groups", null, values);
			getContext().getContentResolver().notifyChange(NOTIFY_URI, null);
			return ContentUris.withAppendedId(uri, id1);
		case THREAD_ID_CODE:
			long id2 = db.insert("thread_group", null, values);
			getContext().getContentResolver().notifyChange(NOTIFY_URI, null);
			return ContentUris.withAppendedId(uri, id2);
		default:
			throw new IllegalArgumentException("无效的URI: " + uri);
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		switch(matcher.match(uri)) {
		case GROUP_ID_CODE:
			long id = ContentUris.parseId(uri);
			int rows1 = db.delete("groups", "_id=?", new String[]{id + ""});
			int rows2 = db.delete("thread_group", "group_id=?", new String[]{id + ""});
			getContext().getContentResolver().notifyChange(NOTIFY_URI, null);
			return rows1 + rows2;
		default:
			throw new IllegalArgumentException("无效的URI: " + uri);
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		switch(matcher.match(uri)) {
		case GROUP_CODE:
			int rows = db.update("groups", values, selection, selectionArgs);
			getContext().getContentResolver().notifyChange(NOTIFY_URI, null);
			return rows;
		default:
			throw new IllegalArgumentException("无效的URI: " + uri);
		}
	}

}
